package com.example.speedwaymanager;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class ChangeActivity extends Activity implements OnItemSelectedListener {

	private CGameManager gm = CGameManager.getInstance();
	int lastPosition = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change);		
		fillTheContent();
	}

	private void fillTheContent() {
		Spinner spinner1 = (Spinner) findViewById(R.id.spinner1_change);
		addDataToSpinner(spinner1, 0);
		Spinner spinner2 = (Spinner) findViewById(R.id.spinner2_change);
		addDataToSpinner(spinner2, 1);
		Spinner spinner3 = (Spinner) findViewById(R.id.spinner3_change);
		addDataToSpinner(spinner3, 2);
		Spinner spinner4 = (Spinner) findViewById(R.id.spinner4_change);
		addDataToSpinner(spinner4, 3);
	}

	/**
	 * @param spinner 
	 * @param rider order
	 */
	private void addDataToSpinner(Spinner spinner, int rider) {
		spinner.setOnItemSelectedListener(this);
		CHeat heat = gm.get_match().get_heats().get(gm.getCurrentHeatNo());
		List<CRider> list = new ArrayList<CRider>();
		list.add(heat.get_riders().get(rider));
		appendAvailableRiders(list,heat,rider);
		//ArrayAdapter<String> data = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
		ArrayAdapter<CRider> data = new ArrayAdapter<CRider>(this, android.R.layout.simple_spinner_item, list);
		data.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(data);
	}

	private void appendAvailableRiders(List<CRider> list, CHeat heat, int rider) {
		CTeam team = CUtils.getTeamByName(heat.get_riders().get(rider).get_team());
		String riderName = heat.get_riders().get(rider).get_name();
		
		for (CRider r: team.get_riders()) {
			if ( r.get_name().equals(riderName) ) {				
			} else {
				list.add(r);
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.change, menu);
		return true;
	}
	
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // An item was selected. You can retrieve the selected item using
		//CRider newRider = (CRider) parent.getItemAtPosition(pos);
		//Toast.makeText(this, newRider.toString() , Toast.LENGTH_SHORT).show();	
    }

    public void onNothingSelected(AdapterView<?> parent) {
    }
    
    public void onCancelChangeClicked(View view)
    {
    	this.finish();
    }
    
    public void onChangeConfirmClick(View view)
    {
    	Spinner spinner1 = (Spinner) findViewById(R.id.spinner1_change);
    	changeRiders(spinner1);		
    	Spinner spinner2 = (Spinner) findViewById(R.id.spinner2_change);
    	changeRiders(spinner2);
    	Spinner spinner3 = (Spinner) findViewById(R.id.spinner3_change);
    	changeRiders(spinner3);
    	Spinner spinner4 = (Spinner) findViewById(R.id.spinner4_change);
    	changeRiders(spinner4);
    	this.finish();
    }

	/**
	 * @param spinner
	 */
	private void changeRiders(Spinner spinner) {
		CRider oldRider = (CRider) spinner.getItemAtPosition(0);
    	CRider newRider = (CRider) spinner.getSelectedItem();
    	CHeat heat = gm.getCurrentHeat();
    	heat.changeRiders(newRider, oldRider);
		CUtils.changeGatesInHeat(gm.get_match(), gm.getCurrentHeatNo(), newRider, oldRider);
	}

}
